package pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author lyx
 * @Date 2021/6/26 9:55
 * Executors(执行者) 三大方法
 * 单例线程/固定线程/伸缩线程
 */
public class ThreadPool {
    public static void main(String[] args) {

        ExecutorService threadPool = Executors.newSingleThreadExecutor();//单例线程 : 1个线程在跑
        ExecutorService threadPool1 = Executors.newFixedThreadPool(10);//固定线程: 1~10个线程在跑
        ExecutorService threadPool2 = Executors.newCachedThreadPool();//伸缩线程 : for设置多少,就跑多少线程

        try {
            for (int i = 0; i < 300; i++) {
                threadPool2.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" ==> Run ok .");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();//使用完关闭池
        }
    }

}
